package com.fzj.example.interface_reference;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * ClassName: ClassLoaderTest
 * date: 2019/10/22 15:28
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class ClassLoaderTest {

    //验证
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Class<Sub> subClass;
        //subClass = (Class< Sub >) Sub.class.getClassLoader().loadClass("com.fzj.example.interface_reference.Sub");
        System.out.println("..............");
        subClass= (Class< Sub >) Class.forName("com.fzj.example.interface_reference.Sub");
        Sub sub=subClass.newInstance();
        Method[] methods=subClass.getMethods();
        Method[] methods1=subClass.getDeclaredMethods();
        for (Method m: methods1) {
            m.setAccessible(true);
            Annotation[] annotations=m.getAnnotations();
            for (Annotation anno: annotations) {
                if(anno instanceof Max){

                }
            }
            if(m.getName().equals("setAge")){
                String str="23";
                m.invoke(sub,"23");
            }
        }
        Field[] fields=subClass.getFields();
        //私有属性
        Field[] fields1=subClass.getDeclaredFields();

    }
}

class Parent{
    private String username;
    public String age;
    public String test;
    static {
        System.out.println("parent static init");
    }


    public String getUsername() {
        return username;
    }

    public String getAge() {
        return age;
    }

    private void setAge(String age) {
        System.out.println("age invoke:" + age);
        this.age = age;
    }
}
class Sub extends  Parent{
    private String username;
    public String age;
    public String test;
    static{
        System.out.println("sub static init");
    }

    public String getUsername() {
        return username;
    }

    public String getAge() {
        return age;
    }

    private void setAge(String age) {
        System.out.println("age invoke:" + age);
        this.age = age;
    }
}
